package com.gitee.loyo.interfaces.impl

import com.gitee.loyo.KSQL

class SelectSQL<T: Any>(private val obj: T, private val select: String = "*", private val tableName: String? = null) : BaseModelSQL<T>() {

    init {
        buildSQL()
    }

    override fun buildSQL() {
        builder.append("select $select from ")
            .append(tableName?:KSQL.nameMappingStrategy.toColumnName(obj.javaClass.simpleName))
            .append(" where ")

        var i = 0
        eachFields(obj){ columnName, value ->
            if(KSQL.nullDetector.isNull(value))
                return@eachFields

            if(i++ > 0){
                builder.append(" and ")
            }
            builder.append(columnName).append(" = ?")
            parameters.add(value)
        }
    }


}